<?php
class SpareApp extends MemberbaseApp
{
	var $_spare_mod;
	var $_demand_mod;
	
	function __construct(){
		parent::__construct();
		$this->SpareApp();
	}
	
	function SpareApp(){
		$this->_spare_mod=&m('spare');
		$this->_demand_mod=&m('package_demand');
	}
	
	//备用扣列表
	function index(){
		$page = $this->_get_page();
		$spare_list = $this->_spare_mod->find(array(
				'conditions' => 'if_show=1',
				'count' => true,
				'limit' => $page['limit']
		));
		$page['item_count'] = $this->_spare_mod->getCount();
		$this->_format_page($page);
		$this->assign('page_info', $page);
		$this->assign('spare_list', $spare_list);
		$this->display('spare.index.html');
		$this->_curlocal(Lang::get('spare_list'));
	}
	
	//添加备用扣位置
	function add(){
		if(!$_POST){
			$this->_curlocal(Lang::get('spare_list'), 'index.php?app=spare&act=index',
					Lang::get('add_spare'));
			$this->display('spare.form.html');
		}else{
			$data=array();
			$spare_position=trim($_POST['spare_position']);
			if (!$spare_position){
				$this->show_warning('spare_position_not_empty');
				return;
			}
			$data['spare_position']=$spare_position;
			$this->_spare_mod->add($data);
			if ($this->_spare_mod->has_error()){
				$this->show_warning($this->_spare_mod->get_error());
				return;
			}
			$this->show_message('添加成功',Lang::get('spare_list'),'index.php?app=spare&act=index');
		}
	}
	
	
	function edit(){
		$spare_id=isset($_GET['spare_id'])?$_GET['spare_id']:0;
		if (!$spare_id){
			$this->show_warning("没有该备用扣位置");
			return;
		}
		if(!$_POST){
			$spare_info=$this->_spare_mod->get($spare_id);
			$this->_curlocal(Lang::get('spare_list'), 'index.php?app=spare&act=index',
					Lang::get('edit_spare'));
			$this->assign('spare',$spare_info);
			$this->display('spare.form.html');
		}else{
			$data=array();
			$spare_position=trim($_POST['spare_position']);
			if (!$spare_position){
				$this->show_warning('spare_position_not_empty');
				return;
			}
			$data['spare_position']=$spare_position;
			$this->_spare_mod->edit($spare_id,$data);
			if ($this->_spare_mod->has_error()){
				$this->show_warning($this->_spare_mod->get_error());
				return;
			}
			$this->show_message('编辑成功',Lang::get('spare_list'),'index.php?app=spare&act=index');
		}
	}
	
	
	function drop(){
		$spare_id=isset($_GET['spare_id'])?$_GET['spare_id']:0;
		if (!$spare_id){
			$this->show_warning("没有该备用扣位置");
			return;
		}
		$this->_spare_mod->drop($spare_id);
		if ($this->_spare_mod->has_error()){
			$this->show_warning($this->_spare_mod->get_error());
			return;
		}
		$this->show_message('删除成功');
	}
	
	
	function package_demand(){
		$page = $this->_get_page();
		$pack_list = $this->_demand_mod->find(array(
				'conditions' => 'if_show=1',
				'count' => true,
				'limit' => $page['limit']
		));
		$page['item_count'] = $this->_demand_mod->getCount();
		$this->_format_page($page);
		$this->assign('page_info', $page);
		$this->assign('pack_list', $pack_list);
		$this->_curlocal(Lang::get('package_demand_list'));
		$this->display('package.index.html');
	}
	
	
	function package_add(){
		if(!$_POST){
			$this->_curlocal(Lang::get('package_demand_list'), 'index.php?app=spare&act=package_demand',
					Lang::get('add_package'));
			$this->display('package.form.html');
		}else{
			$data=array();
			$package_demand=trim($_POST['package_demand']);
			if (!$package_demand){
				$this->show_warning('package_demand_not_empty');
				return;
			}
			$data['package_demand']=$package_demand;
			$this->_demand_mod->add($data);
			if ($this->_demand_mod->has_error()){
				$this->show_warning($this->_demand_mod->get_error());
				return;
			}
			$this->show_message('添加成功',Lang::get('package_demand_list'),'index.php?app=spare&act=package_demand');
		}
	}
	
	
	function package_edit(){
		$pack_id=isset($_GET['pack_id'])?$_GET['pack_id']:0;
		if (!$pack_id){
			$this->show_warning("没有该包装要求");
			return;
		}
		if(!$_POST){
			$package_info=$this->_demand_mod->get($pack_id);
			$this->_curlocal(Lang::get('package_demand_list'), 'index.php?app=spare&act=package_demand',
					Lang::get('edit_package'));
			$this->assign('packs',$package_info);
			$this->display('package.form.html');
		}else{
			$data=array();
			$package_demand=trim($_POST['package_demand']);
			if (!$package_demand){
				$this->show_warning('package_demand_not_empty');
				return;
			}
			$data['package_demand']=$package_demand;
			$this->_demand_mod->edit($pack_id,$data);
			if ($this->_demand_mod->has_error()){
				$this->show_warning($this->_demand_mod->get_error());
				return;
			}
			$this->show_message('编辑成功',Lang::get('package_demand_list'),'index.php?app=spare&act=package_demand');
		}
	}
	
	
	function package_drop(){
		$pack_id=isset($_GET['pack_id'])?$_GET['pack_id']:0;
		if (!$pack_id){
			$this->show_warning("没有该包装要求");
			return;
		}
		$this->_demand_mod->drop($pack_id);
		if ($this->_demand_mod->has_error()){
			$this->show_warning($this->_demand_mod->get_error());
			return;
		}
		$this->show_message('删除成功');
	}
}